import sensor, image, time
from time import sleep
from os import listdir
from math import floor,ceil

class FINDPYFILE:
  def __init__(self):
    self.py_file_quantity=0              #file数量
    self.py_file_names=listdir('')       #扫描根目录下程序
    self.search_filename()               #扫描py文件

  def search_filename(self):
    self.py_file_names.sort()#文件排序
    while self.py_file_quantity < len(self.py_file_names)-1:#删除非.py文件
        for self.py_file_quantity in range(len(self.py_file_names)):
            if not (self.py_file_names[self.py_file_quantity][-3:]=='.py' or \
                  self.py_file_names[self.py_file_quantity][-3:]=='.PY') or \
                  self.py_file_names[self.py_file_quantity]=='main.py' or \
                  self.py_file_names[self.py_file_quantity]=='boot.py' or \
                  self.py_file_names[self.py_file_quantity]=='button.py' or \
                  self.py_file_names[self.py_file_quantity]=='findpyfile.py' or \
                  self.py_file_names[self.py_file_quantity]=='openmv.config':
                del self.py_file_names[self.py_file_quantity]
                break
    #如果倒数第二个文件不是py，删除后不会遍历最后一个文件，下面再遍历一遍解决这个BUG
    for self.py_file_quantity in range(len(self.py_file_names)):
        if not (self.py_file_names[self.py_file_quantity][-3:]=='.py' or \
              self.py_file_names[self.py_file_quantity][-3:]=='.PY') or \
              self.py_file_names[self.py_file_quantity]=='main.py' or \
              self.py_file_names[self.py_file_quantity]=='boot.py' or \
              self.py_file_names[self.py_file_quantity]=='button.py' or \
              self.py_file_names[self.py_file_quantity]=='findpyfile.py' or \
              self.py_file_names[self.py_file_quantity]=='openmv.config':
            del self.py_file_names[self.py_file_quantity]
            break

    for self.py_file_quantity in range(len(self.py_file_names)):#删除扩展名
        self.py_file_names[self.py_file_quantity]= \
          self.py_file_names[self.py_file_quantity][:-3]
    self.py_file_quantity=len(self.py_file_names)
